#!/bin/sh
#
# $Id: postinst 46 2011-11-29 17:01:09Z imooreyahoo@gmail.com $
#
# see: dh_installdeb(1)

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

case "$1" in
    configure)

	# Check for XML config
	if ! omv_config_exists "//services/dnsmasq"; then
		omv_config_add_element "//services" "dnsmasq" ""
	fi
	
    if ! omv_config_exists "//services/dnsmasq/dhcp-enable"; then
        omv_config_add_element "//services/dnsmasq" "dhcp-enable" "0"
    fi

    if ! omv_config_exists "//services/dnsmasq/extraoptions"; then
        omv_config_add_element "//services/dnsmasq" "extraoptions" ""
    fi

    if ! omv_config_exists "//services/dnsmasq/domain-name"; then
        omv_config_add_element "//services/dnsmasq" "domain-name" "local"
    fi

    if ! omv_config_exists "//services/dnsmasq/network"; then
        omv_config_add_element "//services/dnsmasq" "network" ""
    fi

    if ! omv_config_exists "//services/dnsmasq/gateway"; then
        omv_config_add_element "//services/dnsmasq" "gateway" ""
    fi

    if ! omv_config_exists "//services/dnsmasq/first-ip"; then
        omv_config_add_element "//services/dnsmasq" "first-ip" ""
    fi

    if ! omv_config_exists "//services/dnsmasq/last-ip"; then
        omv_config_add_element "//services/dnsmasq" "last-ip" ""
    fi

    if ! omv_config_exists "//services/dnsmasq/default-lease-time"; then
        omv_config_add_element "//services/dnsmasq" "default-lease-time" "168h"
    fi

    if ! omv_config_exists "//services/dnsmasq/wins-servers"; then
        omv_config_add_element "//services/dnsmasq" "wins-servers" ""
    fi

    if ! omv_config_exists "//services/dnsmasq/ntp-servers"; then
        omv_config_add_element "//services/dnsmasq" "ntp-servers" ""
    fi

    if ! omv_config_exists "//services/dnsmasq/bootfile"; then
        omv_config_add_element "//services/dnsmasq" "bootfile" ""
    fi

    if ! omv_config_exists "//services/dnsmasq/dns-log-queries"; then
        omv_config_add_element "//services/dnsmasq" "dns-log-queries" "0"
    fi

    if ! omv_config_exists "//services/dnsmasq/log-dhcp"; then
        omv_config_add_element "//services/dnsmasq" "log-dhcp" "0"
    fi

    if ! omv_config_exists "//services/dnsmasq/dns-wins"; then
        omv_config_add_element "//services/dnsmasq" "dns-wins" "0"
    fi

    if ! omv_config_exists "//services/dnsmasq/enable"; then
        omv_config_add_element "//services/dnsmasq" "enable" "0"
    fi

    if ! omv_config_exists "//services/dnsmasq/entries"; then
        omv_config_add_element "//services/dnsmasq" "entries" ""
    fi
	
	# Fix permissions
	omv_install_fixperms
	
	# Reload rsyslogd so that it sees the new config file
	/etc/init.d/rsyslog restart

	
	chown root /usr/local/bin/dnsmasq_wins
	chmod 0700 /usr/local/bin/dnsmasq_wins

	# Backup dnsmasq config
	if [ ! -e /etc/default/dnsmasq-orig ]; then
		/bin/cp /etc/default/dnsmasq /etc/default/dnsmasq-orig
	fi
	
	# Run config and restart dnsmasq
	omv-mkconf dnsmasq 2>/dev/null
	/etc/init.d/dnsmasq restart || true
	
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
